public class LongestSubstring {
    public static void main(String[] args) {
        String s1 ="aaabb";
        int k1 =3;
        String s2 ="ababbc";
        int k2 =2;
        LongestSubstring longestSubstring = new LongestSubstring();
        System.out.println(longestSubstring.longestSubstring(s1, k1));
        System.out.println(longestSubstring.longestSubstring(s2, k2));

    }
    public int longestSubstring(String s, int k) {
       int length =0;

        for (int i = 0; i < s.length(); i++) {
            for (int j = s.length()-1; j >=i ; j--) {
                int[] lArr = new int[26];
                for (int l = i; l <=j ; l++) {
                    lArr[s.charAt(l)-'a'] = lArr[s.charAt(l)-'a'] +1;
                }
               if(check(lArr,k)){
                   if((j - i + 1) >length ){
                       length = j - i +1;
                   }
               }
            }
        }
       return length;
    }
    public boolean check(int[]lArr, int k) {
        boolean result = true;
        for (int i = 0; i < lArr.length; i++) {
            if(lArr[i] !=0 && lArr[i] <k){
                result = false;
            }
        }
        return result;
    }
}
